/*
 *
 * 名称：方案
 * 功能：奇门遁甲断局方案配置
 * 作者：清河张
 * 时间：2025.05.11
 *
 */

#include "fangan.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>

using namespace QMDJ;


FANGAN::FANGAN(QMGONG9& g9):
    _confgfile(""), _gong9(g9)
{
}


const QByteArray& FANGAN::duan(LIFA sr)
{
    int pf1, gxs, gxs0, pf2;
    WUXING5::SHENGKE sk;
    QJsonArray&& jsonGX = _jsonObj[YSGUANXI].toArray();

    _duanstr = "\n銝㔉奇门，只供排盘，不做论断，以下提示，来自方案： “";
    _duanstr += _jsonObj[YSFANGAN].toString().toUtf8();
    _duanstr += "”\n\n【——单个用神分析——】\n\n";

    for(auto itor = _ysmap.begin(); itor != _ysmap.end(); ++itor)
    {
        YONGSHEN & ys = itor->second;
        ys._lg = _gong9.yongshen(ys._buluo, ys._bianhao, sr.bazi(),
                                 ys._ming, ys._bianzhi, ys._nengji);
        if( nullptr == ys._lg)
        {
            _duanstr += "【";
            _duanstr += ys._ming;
            _duanstr += "】用神找不到";
            continue;
        }

        _duanstr += ys._ming;
        _duanstr += " 取“";
        _duanstr += ys._bianzhi;
        _duanstr += "” 能量“";
        _duanstr += ys._nengji;
        _duanstr += "”：";
        _duanstr += ys._xiangyi;
        _duanstr += "\n";
        _duanstr += ys._lg->_jiexi;
        _duanstr += "\n";
    }

    _duanstr += "\n【——用神关系分析——】\n";

    for(int row = 0; row < jsonGX.count(); ++row)
    {
        QJsonObject&& gx= jsonGX[row].toObject();
        pf1 = gx[YSPAIFU1].toInt();
        gxs = gx[YSGUANXIS].toInt();
        pf2 = gx[YSPAIFU2].toInt();
        if(_ysmap.find(pf1) == _ysmap.end())
        {
            _duanstr += "找不到用神1【";
            _duanstr += QByteArray::number(pf2);
            _duanstr += "】\n";
            continue;
        }
        if(_ysmap.find(pf2) == _ysmap.end())
        {
            _duanstr += "找不到用神2【";
            _duanstr += QByteArray::number(pf2);
            _duanstr += "】\n";
            continue;
        }

        YONGSHEN& ys1 = _ysmap[pf1];
        YONGSHEN& ys2 = _ysmap[pf2];
        sk = ys1._lg->wx5() - ys2._lg->wx5();

        gxs0 = (ys1._lg == ys2._lg) ? 5 : sk.id() ;
        if ( gxs != gxs0 )
            continue;

        _duanstr += "\n【“";
        _duanstr += ys1._ming;
        _duanstr += "”，";
        _duanstr += ys1._nengji;

        if(ys1._lg->hasmask(QMGONG9::MASK_MENPO))
        {
            _duanstr += "，门迫：";
            _duanstr += ys1._menpo;
        }
        if(ys1._lg->hasmask(QMGONG9::MASK_RUMU2))
        {
            _duanstr += "，入墓：";
            _duanstr += ys1._rumu;
        }
        if(ys1._lg->hasmask(QMGONG9::MASK_JIXING2))
        {
            _duanstr += "，击刑：";
            _duanstr += ys1._jixing;
        }
        if(ys1._lg->hasmask(QMGONG9::MASK_KONGWANG1 | QMGONG9::MASK_KONGWANG2))
        {
            _duanstr += "，空亡：";
            _duanstr += ys1._kongwang;
        }
        _duanstr += "】=>【“";
        _duanstr += ys2._ming;
        _duanstr += "”，";
        _duanstr += ys2._nengji;

        if(ys2._lg->hasmask(QMGONG9::MASK_MENPO))
        {
            _duanstr += "，门迫：";
            _duanstr += ys2._menpo;
        }
        if(ys2._lg->hasmask(QMGONG9::MASK_RUMU2))
        {
            _duanstr += "，入墓：";
            _duanstr += ys2._rumu;
        }
        if(ys2._lg->hasmask(QMGONG9::MASK_JIXING2))
        {
            _duanstr += "，击刑：";
            _duanstr += ys2._jixing;
        }
        if(ys2._lg->hasmask(QMGONG9::MASK_KONGWANG1 | QMGONG9::MASK_KONGWANG2))
        {
            _duanstr += "，空亡：";
            _duanstr += ys2._kongwang;
        }
        _duanstr += "】\n【";
        _duanstr += (ys1._lg == ys2._lg) ? "逢" : sk.name();
        _duanstr += "】的关系：";
        _duanstr += gx[YSGXXY].toString().toUtf8();

        _duanstr += "\n";
    }

    _duanstr += "\n";

    return _duanstr;
}

void FANGAN::remove(qint64 id)
{
    QString fn = "qm/";
    fn += QString::number(id) + ".json";
    QFile::remove(fn);
}

bool FANGAN::load(qint64 id)
{
    _confgfile = QString("qm/") + QString::number(id) + ".json";
    return load();
}


bool FANGAN::load()
{
    QJsonDocument jsonDoc;

    QFile qfile( _confgfile);

    _jsonObj = QJsonObject();

    if(!qfile.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    jsonDoc = QJsonDocument::fromJson(qfile.readAll());

    qfile.close();

    if (jsonDoc.isNull())
        return false;

    if(jsonDoc.isEmpty())
        return false;

    _jsonObj = jsonDoc.object();

    setys();

    return true;
}

bool FANGAN::store(const QJsonObject& jo)
{
    QJsonDocument jsonDoc;

    QFile qfile(_confgfile);

    _jsonObj = jo;

    setys();

    jsonDoc.setObject(jo);

    QByteArray jsons = jsonDoc.toJson();

    if(!qfile.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;

    qfile.write(jsons);

    qfile.close();

    return true;
}

const QJsonObject &FANGAN::jsonObj()
{
    return _jsonObj;
}

QMGONG9 &FANGAN::gong9()
{
    return _gong9;
}

const FANGAN::YSMAP &FANGAN::ysmap() const
{
    return _ysmap;
}

const FANGAN::YONGSHEN *FANGAN::ys(unsigned pf) const
{
    const YONGSHEN* ys = nullptr;
    auto itor = _ysmap.find(pf);
    if(itor != _ysmap.end())
        ys = &itor->second;

    return ys;
}

void FANGAN::setys()
{
    _ysmap.clear();
    unsigned pf;

    _gong9.shezhifangan(_jsonObj[YSGEJU].toObject(), _jsonObj[YSXINGGONG].toObject());

    QJsonArray&& jsonYS = _jsonObj[YSFIELD].toArray();

    for(int row = 0; row < jsonYS.count(); ++row)
    {
        QJsonObject&& jys = jsonYS[row].toObject();

        YONGSHEN ys;
        pf = (unsigned)jys[YSPAIFU].toInt();
        ys._paifu = pf;
        ys._ming = jys[YSMING].toString().toUtf8();
        ys._buluo = (unsigned)jys[YSBULUO].toInt();
        ys._bianhao = jys[YSBIANHAO].toInt();
        ys._jixing = jys[YSXINGJI].toString().toUtf8();
        ys._kongwang = jys[YSKONGWANG].toString().toUtf8();
        ys._menpo = jys[YSMENPO].toString().toUtf8();
        ys._rumu = jys[YSRUMU].toString().toUtf8();
        ys._xiangyi = jys[XIANGYI].toString().toUtf8();
        ys._lg = nullptr;
        _ysmap.insert( std::pair(pf, ys) );
    }
}

